<?php

namespace ticket\modules\frontend\models;

use Yii;

/**
 * This is the model class for table "tk_ticket_log".
 *
 * @property int $id
 * @property string $sd_order_sn 深大订单号
 * @property string $self_order_sn 订单号
 * @property string $assist_check 辅助码
 * @property string $order_price 金额
 * @property int $quantity 数量
 * @property string $occ_time 下单时间
 * @property string $tickname 门票名称
 * @property string $goods_code 订单码
 * @property string $send_content 发送内容
 * @property string $return_content 返回内容
 * @property string $create_time 创建时间
 */
class TicketLog extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tk_ticket_log';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['order_price'], 'number'],
            [['quantity'], 'integer'],
            [['occ_time', 'create_time'], 'required'],
            [['occ_time', 'create_time'], 'safe'],
            [['send_content', 'return_content'], 'string'],
            [['sd_order_sn', 'self_order_sn', 'tickname', 'goods_code'], 'string', 'max' => 64],
            [['assist_check'], 'string', 'max' => 32],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'sd_order_sn' => 'Sd Order Sn',
            'self_order_sn' => 'Self Order Sn',
            'assist_check' => 'Assist Check',
            'order_price' => 'Order Price',
            'quantity' => 'Quantity',
            'occ_time' => 'Occ Time',
            'tickname' => 'Tickname',
            'goods_code' => 'Goods Code',
            'send_content' => 'Send Content',
            'return_content' => 'Return Content',
            'create_time' => 'Create Time',
        ];
    }


    public function addInfo($data,$send_content)
    {
        // $data['orderResponse']['order']['orderCode']
        $order = $data['orderResponse']['order'];
        $list['TicketLog']['create_time'] = date("Y-m-d H:i:s");
        $list['TicketLog']['occ_time'] = date("Y-m-d H:i:s");
        $list['TicketLog']['sd_order_sn'] = isset($order['orderCode'])?$order['orderCode']:'';
        $list['TicketLog']['self_order_sn'] = $data['order_sn'];
        $list['TicketLog']['assist_check'] = isset($order['assistCheckNo'])?($order['assistCheckNo']):'';
        $list['TicketLog']['order_price'] = $order['orderPrice'];
        $list['TicketLog']['quantity'] = (int)$order['ticketOrders']['ticketOrder']['quantity'];
        $list['TicketLog']['goods_code'] = $order['ticketOrders']['ticketOrder']['goodsCode'];
        $list['TicketLog']['send_content'] = json_encode($send_content,JSON_UNESCAPED_UNICODE);
        $list['TicketLog']['return_content'] = json_encode($data,JSON_UNESCAPED_UNICODE);
        if ($this->load($list) and $this->validate()){
            if ($this->save(0)){
                return true;
            }
        }
        return false;

    }

}
